var router = require('koa-router')();
import config from '../config';
var json2xls = require('json2xls');
import querystring from 'querystring';

router.post('/list',async (ctx, next)=>{
	let {
		nickname='',
		idNumber='',
		phone='',
		startTime='',
		endTime = '',
		page = 1,
		size=10
	} = ctx.request.body;

	let count = await ctx.post('/admin/countUser',{
				nickname,
				idNumber,
				phone,
				startTime,
				endTime,
				page,
			});


	let result = await ctx.post('/admin/listUser',{
				nickname,
				idNumber,
				phone,
				startTime,
				endTime,
				page,
				size
			});
	if(count.cd==0&&result.cd==0){
		result.total = count.data;
	}

    ctx.body = result;
});

//导出
router.get('/export',async (ctx, next)=>{
	let {
		search
	} = ctx.request.query;
	var {
		nickname='',
		idNumber='',
		phone='',
		startTime='',
		endTime = '',
	} = JSON.parse(decodeURIComponent(search));
	
	let result = await ctx.post('/admin/listAllUser',{
					nickname,
					idNumber,
					phone,
					startTime,
					endTime
				});
	var data = []
	if(result.cd==0){
		result.data.forEach((item,index)=>{
			data.push({
				'用户编号':item.id,
				'用户昵称':item.nickname,
				'微信号':item.wechatId,
				'用户手机号':item.phone,
				'身份证号':item.idNumber,
				'注册时间':item.registerTime
			})
		})
	}
	if(data.length==0){
		data.push({
				'用户编号':'',
				'用户昵称':'',
				'微信号':'',
				'用户手机号':'',
				'身份证号':'',
				'注册时间':''
			})
	}
	ctx.set('Content-Type', 'application/octet-stream;charset=utf-8');
	ctx.set("Content-Disposition", "attachment; filename="+encodeURIComponent('用户信息')+".xlsx");
	// Buffer.concat([new Buffer('\xEF\xBB\xBF', 'binary'), new Buffer(json2xls(data))]);
    ctx.body = new Buffer(json2xls(data),'binary')
});


//用户信息
router.post('/find/user', async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/findUserById',{id});
});

//账户信息
router.post('/find/account', async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/findAccountInfoById',{id});
})

//更换银行卡
router.post('/account/changeBankCardNo', async (ctx,next) => {
	let {id,bankCardNo} = ctx.request.body;
	ctx.body = await ctx.post('/admin/changeBankCardNo',{id,bankCardNo});
})

//投资记录
router.post('/invest/record',async (ctx,next) => {
	let {id,page=1,size=10} = ctx.request.body;
	let count = await ctx.post('/admin/countInvestRecordById',{id});
	let result = await ctx.post('/admin/findInvestRecordById',{id,page,size});
	if(count.cd==0&&result.cd==0){
		result.total = count.data
	}
	ctx.body = result
})

//筹款记录
router.post('/raise/record',async (ctx,next) => {
	let {id,page=1,size=10} = ctx.request.body;
	let count = await ctx.post('/admin/countFundraisingRecordById',{id});
	let result = await ctx.post('/admin/findFundraisingRecordById',{id,page,size});
	if(count.cd==0&&result.cd==0){
		result.total = count.data
	}
	ctx.body = result
})

//资金记录
router.post('/funds/record',async (ctx,next) => {
	let {id,page=1,size=10} = ctx.request.body;
	let count = await ctx.post('/admin/countCapitalChangeById',{id});
	let result = await ctx.post('/admin/findCapitalChangeById',{id,page,size});
	if(count.cd==0&&result.cd==0){
		result.total = count.data
	}
	ctx.body = result
})
//资金手工确认
router.post('/funds/confirm',async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/modifyCapitalChangeStatus',{id});
})

//短信记录
router.post('/sms/record',async (ctx,next) => {
	let {phone,page=1,size=10} = ctx.request.body;
	let count = await ctx.post('/admin/countMessageByReceiver',{phone});
	let result = await ctx.post('/admin/listMessageByReceiver',{phone,page,size});
	if(count.cd==0&&result.cd==0){
		result.total = count.data
	}
	ctx.body = result
})

//发送短信
router.post('/sendsms',async (ctx,next) => {
	let {receiverStr,content,type} = ctx.request.body;
	ctx.body = await ctx.post('/admin/sendMessage',{receiverStr,content,type});
})
//短信手工确认
router.post('/sendsms/confirm' , async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/modifyMsgStatus',{id});
})


/** 提现  start **/

//列表
router.post('/withdraw/list' , async (ctx,next) => {
	let {
		page=1,
		size=10,
		realName='',
		startTime='',
		endTime = '',
		phone = '',
		status = ''
	} = ctx.request.body;
	let params = {realName,startTime,endTime,phone,status};
	let count = await ctx.post('/admin/listDepositWithdraw',params);
	params.page = page;
	params.size = size;
	let result = await ctx.post('/admin/listDepositWithdraw',params);
	if(count.cd==0&&result.cd==0){
		result.total = count.data;
	}
	ctx.body = result;
});
//详情
router.post('/withdraw/detail' , async (ctx,next) => {
	let {id} = ctx.request.body;
	let withdrawInfo = await ctx.post('/admin/findDepositWithdrawById',{id});
	let userInfo = {cd:1,msg:'查询用户信息失败'};
	if(withdrawInfo.cd==0){
		userInfo = await ctx.post('/admin/findAccountInfoById' , {id:withdrawInfo.data.user_no});
	}
	ctx.body = {withdrawInfo,userInfo}
})
//放款
router.post('/withdraw/lending' , async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/updateDepositWithdrawStatus',{id});
})
/** 提现  end **/


export default router;
